{
 "metadata": {
  "name": "",
  "signature": "sha256:444e33367fb1bd3a73bbbf6b4310ca75cdb7f8591d9ee3a486f36af979983692"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "from __future__ import print_function\n",
      "from copy import deepcopy\n",
      "\n",
      "\n",
      "class DataStore(object):\n",
      "    def __init__(self, mask=None):\n",
      "        self.mask = [] if mask is None else mask\n",
      "    \n",
      "    def state_of_data(self):\n",
      "        return {}\n",
      "    \n",
      "    def __iter__(self):\n",
      "        print(\"DataStore.__iter__\")\n",
      "        self.i = -1\n",
      "        return self\n",
      "    \n",
      "    def next(self):\n",
      "        self.i += 1\n",
      "        while self.i in self.mask:\n",
      "            self.i += 1\n",
      "        if self.i < 10:\n",
      "            return self.i\n",
      "        else:\n",
      "            raise StopIteration\n",
      "            \n",
      "    \n",
      "class FailedPreconditionError(Exception):\n",
      "    pass\n",
      "\n",
      "\n",
      "class Node(object):\n",
      "    def __init__(self, upstream):\n",
      "        self.upstream = upstream.__iter__()\n",
      "        \n",
      "    def state_of_data(self):\n",
      "        upstream_condition = deepcopy(self.upstream.state_of_data())\n",
      "        upstream_condition.update(self.__class__.outcome)\n",
      "        return upstream_condition\n",
      "    \n",
      "    def check_preconditions(self):\n",
      "        requirements = self.__class__.__dict__.get('requirements')\n",
      "        if requirements:\n",
      "            condition = self.upstream.state_of_data()\n",
      "            for k,v in requirements.iteritems():\n",
      "                if not condition.has_key(k):\n",
      "                    raise FailedPreconditionError()\n",
      "                if condition[k] != v:\n",
      "                    raise FailedPreconditionError()                    \n",
      "\n",
      "    def run(self):\n",
      "        \"\"\"Pulls data through the pipeline.  Useful if we just want to calculate \n",
      "        some stats.\"\"\"\n",
      "        for x in self:\n",
      "            pass\n",
      "\n",
      "            \n",
      "class AddOne(Node):\n",
      "    outcome = {'added_one': True}\n",
      "    requirements = {'multiplied_by_ten': True}\n",
      "    \n",
      "    def __iter__(self):\n",
      "        print(\"AddOne.__iter__\")\n",
      "        self.check_preconditions()\n",
      "        self.results = 0\n",
      "        return self\n",
      "        \n",
      "    def next(self):\n",
      "        i = next(self.upstream)\n",
      "        output = i+1\n",
      "        self.results += output\n",
      "        return output\n",
      "            \n",
      "\n",
      "class MultiplyByTen(Node):\n",
      "    outcome = {'multiplied_by_ten': True}\n",
      "\n",
      "    def __iter__(self):\n",
      "        print(\"MultiplyByTen.__iter__\")\n",
      "        self.check_preconditions()    \n",
      "        return self\n",
      "    \n",
      "    def next(self):\n",
      "        i = next(self.upstream)\n",
      "        return i*10\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 50
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "ds = DataStore([4,6])\n",
      "for d in ds:\n",
      "    print(d)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "DataStore.__iter__\n",
        "0\n",
        "1\n",
        "2\n",
        "3\n",
        "5\n",
        "7\n",
        "8\n",
        "9\n"
       ]
      }
     ],
     "prompt_number": 51
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "ds = DataStore([3,5,7])\n",
      "mult = MultiplyByTen(ds)\n",
      "addone = AddOne(mult)\n",
      "addone.run()\n",
      "addone.results"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "DataStore.__iter__\n",
        "MultiplyByTen.__iter__\n",
        "AddOne.__iter__\n"
       ]
      },
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 52,
       "text": [
        "307"
       ]
      }
     ],
     "prompt_number": 52
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# Train disaggregator\n",
      "\n",
      "for meter in metergroup.meters:\n",
      "    preprocessing = [Clip(), Resample(5)]\n",
      "    for chunk in meter.power_series(preprocessing=preprocessing):\n",
      "        disaggregator.train(meter, chunk)\n",
      "    \n",
      "        # and if the disaggregator needs aggregate power as well:\n",
      "        mains_chunk = mains.power_series(periods=[chunk.timeframe]).next()\n",
      "        disaggregator.train(meter, chunk, mains_chunk)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "ename": "NameError",
       "evalue": "name 'metergroup' is not defined",
       "output_type": "pyerr",
       "traceback": [
        "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
        "\u001b[1;32m<ipython-input-43-e0f8408d1588>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[1;31m# Train disaggregator\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      2\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m \u001b[1;32mfor\u001b[0m \u001b[0mmeter\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mmetergroup\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mmeters\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      4\u001b[0m     \u001b[0mpreprocessing\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m[\u001b[0m\u001b[0mClip\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mResample\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m5\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      5\u001b[0m     \u001b[1;32mfor\u001b[0m \u001b[0mchunk\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mmeter\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mpower_series\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mpreprocessing\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mpreprocessing\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
        "\u001b[1;31mNameError\u001b[0m: name 'metergroup' is not defined"
       ]
      }
     ],
     "prompt_number": 43
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# Disaggregation and metrics (via disk)\n",
      "\n",
      "for chunk in mains.power_series():\n",
      "    appliance_estimates_for_chunk = disaggregator.disaggregate(chunk) # get dataframe\n",
      "    append_to_hdf(appliance_estimates_for_chunk)\n",
      "    \n",
      "# Metrics - take two MeterGroups?  \n",
      "# One for ground truth, one for disag estimates\n",
      "\n",
      "    "
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    }
   ],
   "metadata": {}
  }
 ]
}